'use strict';

const gulp = require('gulp');
const gload = require('gulp-load-plugins');

/**
 * Register gulp plugins
 *
 * @var {object} plugins
 * @var {boolean} plugins.debug
 * @var {string[]} plugins.pattern - glob to search for
 * @var {string[]} plugins.scope - dependencies from package.json
 * @var {boolean} plugins.lazy - lazy load the plugin
 */
const plugins = gload({
  debug: true,
  pattern: ['gulp-*'],
  scope: ['devDependencies'],
  lazy: false,
  rename: { 'gulp-markdown-docs': 'markdownDoc',
            'gulp-sass-bulk-import': 'globify' },
});

/*
 * Register gulp tasks
 */
const getTask = (taskName) => require('./tasks/' + taskName)(gulp, plugins);

/*
 * Define tasks
 */

/**
 * Register single tasks
 */
gulp.task('sass', getTask('sass'));
gulp.task('cssmin', getTask('cssmin'));
gulp.task('babel', getTask('babel'));
gulp.task('eslint', getTask('eslint'));
gulp.task('zip', getTask('zip'));
gulp.task('styleguide:server', () => getTask('styleguideServer'));
gulp.task('static:server', () => getTask('staticServer'));
gulp.task('styleguide:style', getTask('styleguideStyle'));
gulp.task('styleguide', getTask('styleguide'));
gulp.task('unitTest', getTask('unitTest'));
gulp.task('html', getTask('html'));

/**
 * Minifying need the sass compilation first
 *
 * @module css
 */
gulp.task('css', ['sass', 'cssmin']);


/**
 * Watch task, rebuild on change
 *
 * @module watch
 */
gulp.task('watch', () => gulp.watch(['src/**/*.*', 'test/**/*Test.js'],
                                     ['build']));

/**
 * Build task, development
 *
 * @module build
 */
gulp.task('build', ['css', 'eslint', 'babel', 'html', 'unitTest']);

/**
 * Deploy
 *
 * @module deploy
 */
gulp.task('deploy', ['build', 'zip']);

/**
 * Default task, build it all
 *
 * @module default
 */
gulp.task('default', ['build', 'watch', 'static:server', 'styleguide:server']);
